import os
from pathlib import Path
from enum import Enum

from PyQt5.QtCore import Qt, QLocale
from PyQt5.QtGui import QGuiApplication, QFont
from qfluentwidgets import (qconfig, QConfig, ConfigItem, OptionsConfigItem, BoolValidator,
                            ColorConfigItem, OptionsValidator, RangeConfigItem, RangeValidator,
                            FolderListValidator, EnumSerializer, FolderValidator, ConfigSerializer, __version__)


class SongQuality(Enum):
    """ Online song quality enumeration class """

    STANDARD = "Standard quality"
    HIGH = "High quality"
    SUPER = "Super quality"
    LOSSLESS = "Lossless quality"


class MvQuality(Enum):
    """ MV quality enumeration class """

    FULL_HD = "Full HD"
    HD = "HD"
    SD = "SD"
    LD = "LD"


class Language(Enum):
    """ Language enumeration """

    CHINESE_SIMPLIFIED = QLocale(QLocale.Chinese, QLocale.China)
    CHINESE_TRADITIONAL = QLocale(QLocale.Chinese, QLocale.HongKong)
    ENGLISH = QLocale(QLocale.English)
    AUTO = QLocale()


class LanguageSerializer(ConfigSerializer):
    """ Language serializer """

    def serialize(self, language):
        return language.value.name() if language != Language.AUTO else "Auto"

    def deserialize(self, value: str):
        return Language(QLocale(value)) if value != "Auto" else Language.AUTO


class Config(QConfig):
    """ Config of application """

    # folders
    musicFolders = ConfigItem(
        "Folders", "LocalMusic", [], FolderListValidator())
    downloadFolder = ConfigItem(
        "Folders", "Download", "download", FolderValidator())

    # online
    onlineSongQuality = OptionsConfigItem(
        "Online", "SongQuality", SongQuality.STANDARD, OptionsValidator(SongQuality), EnumSerializer(SongQuality))
    onlinePageSize = RangeConfigItem(
        "Online", "PageSize", 30, RangeValidator(0, 50))
    onlineMvQuality = OptionsConfigItem(
        "Online", "MvQuality", MvQuality.FULL_HD, OptionsValidator(MvQuality), EnumSerializer(MvQuality))

    # main window
    enableAcrylicBackground = ConfigItem(
        "MainWindow", "EnableAcrylicBackground", False, BoolValidator())
    minimizeToTray = ConfigItem(
        "MainWindow", "MinimizeToTray", True, BoolValidator())
    playBarColor = ColorConfigItem("MainWindow", "PlayBarColor", "#225C7F")
    recentPlaysNumber = RangeConfigItem(
        "MainWindow", "RecentPlayNumbers", 300, RangeValidator(10, 300))
    dpiScale = OptionsConfigItem(
        "MainWindow", "DpiScale", "Auto", OptionsValidator([1, 1.25, 1.5, 1.75, 2, "Auto"]), restart=True)
    language = OptionsConfigItem(
        "MainWindow", "Language", Language.AUTO, OptionsValidator(Language), LanguageSerializer(), restart=True)

    # desktop lyric
    deskLyricHighlightColor = ColorConfigItem(
        "DesktopLyric", "HighlightColor", "#0099BC")
    deskLyricFontSize = RangeConfigItem(
        "DesktopLyric", "FontSize", 50, RangeValidator(15, 50))
    deskLyricStrokeSize = RangeConfigItem(
        "DesktopLyric", "StrokeSize", 5, RangeValidator(0, 20))
    deskLyricStrokeColor = ColorConfigItem(
        "DesktopLyric", "StrokeColor", Qt.black)
    deskLyricFontFamily = ConfigItem(
        "DesktopLyric", "FontFamily", "Microsoft YaHei")
    deskLyricAlignment = OptionsConfigItem(
        "DesktopLyric", "Alignment", "Center", OptionsValidator(["Center", "Left", "Right"]))

    # software update
    checkUpdateAtStartUp = ConfigItem(
        "Update", "CheckUpdateAtStartUp", True, BoolValidator())

    @property
    def desktopLyricFont(self):
        """ get the desktop lyric font """
        font = QFont(self.deskLyricFontFamily.value)
        font.setPixelSize(self.deskLyricFontSize.value)
        return font

    @desktopLyricFont.setter
    def desktopLyricFont(self, font: QFont):
        dpi = QGuiApplication.primaryScreen().logicalDotsPerInch()
        self.deskLyricFontFamily.value = font.family()
        self.deskLyricFontSize.value = max(15, int(font.pointSize()*dpi/72))
        self.save()


YEAR = 2023
AUTHOR = "chenliang"
VERSION = __version__
HELP_URL = "https://pyqt-fluent-widgets.readthedocs.io"
FEEDBACK_URL = "https://github.com/zhiyiYo/PyQt-Fluent-Widgets/issues"
RELEASE_URL = "https://github.com/zhiyiYo/PyQt-Fluent-Widgets/releases/latest"
REPO_URL = "https://github.com/zhiyiYo/PyQt-Fluent-Widgets"
EXAMPLE_URL = "https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/master/examples"


cfg = Config()
qconfig.load('config/config.json', cfg)

CONFIG_PATH = Path(__file__).resolve().parent
OBJ_PATH = CONFIG_PATH.parent
CONFIG_JSON = CONFIG_PATH / 'config.json'

user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1"
web_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
sec_ua = "\"Not_A_Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\""

dy_cookie = "my_rd=2; ttwid=1%7CUG-omwUhJquIZkGzIHUd_VE3t_iaGnomXx_iyF91qiM%7C1713267476%7Cd6af0baebe40b7f702ee2808a60f29abf46ae31d30c806d9fa4e105ccab9ec68; dy_swidth=2560; dy_sheight=1440; passport_csrf_token=881322d1c357aa85ff3cfb5b16db055a; passport_csrf_token_default=881322d1c357aa85ff3cfb5b16db055a; bd_ticket_guard_client_web_domain=2; xgplayer_user_id=841443396117; vdg_s=1; download_guide=%223%2F20240418%2F1%22; pwa2=%220%7C0%7C3%7C0%22; __ac_nonce=06627e0250086f40535d4; __ac_signature=_02B4Z6wo00f011.1zsQAAIDA984mEudnLB9f1cpAALHlp9pENHRu4gMRs.IswJgaBN498295wPH91Z5zmK5QKeja2bmIX.jk3jnjXaUAsTfHUZIhjZu7DhtJ.1eTs5E4kMG7gHQdz-PM4gZV3e; douyin.com; xg_device_score=7.565564628297558; device_web_cpu_core=20; device_web_memory_size=8; architecture=amd64; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A2560%2C%5C%22screen_height%5C%22%3A1440%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A20%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A50%7D%22; csrf_session_id=0a11ab23be740308cfb00125f163d396; strategyABtestKey=%221713889318.713%22; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Atrue%2C%22volume%22%3A0.4%7D; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%2C%22isForcePopClose%22%3A1%7D; passport_assist_user=Cjw0yeoFObl6IqdfFCa0QVE_kcYkE1kbHRpikFyQBIoNBmqEOjGC-17rvQweimKFdyhFITi7OIiovWj6YeIaSgo8Q094ium3jTjVOMSRYZupA4ri8HFvCigfvv-ElpuSlIlvIGAf06IzomRr-vvwUho8nTOzegZDAXjqm0kbEKu6zw0Yia_WVCABIgED_R9Ccg%3D%3D; n_mh=vbvvaVvMvyp233dHkLKqRbiwVf9B_YcyQfk3XtXJJHI; sso_uid_tt=36ef13af5fbc119d4d500bd684db43d0; sso_uid_tt_ss=36ef13af5fbc119d4d500bd684db43d0; toutiao_sso_user=a152146ad2a00e25d8bed438243c58cb; toutiao_sso_user_ss=a152146ad2a00e25d8bed438243c58cb; sid_ucp_sso_v1=1.0.0-KDk0MzZhNDJhMTE4MTIxYzYxY2Y4NWRhMzJhM2FlMzE2YzI5OGVhODUKHQiBi9DU8QIQvsCfsQYY7zEgDDCH3qbYBTgGQPQHGgJsZiIgYTE1MjE0NmFkMmEwMGUyNWQ4YmVkNDM4MjQzYzU4Y2I; ssid_ucp_sso_v1=1.0.0-KDk0MzZhNDJhMTE4MTIxYzYxY2Y4NWRhMzJhM2FlMzE2YzI5OGVhODUKHQiBi9DU8QIQvsCfsQYY7zEgDDCH3qbYBTgGQPQHGgJsZiIgYTE1MjE0NmFkMmEwMGUyNWQ4YmVkNDM4MjQzYzU4Y2I; passport_auth_status=0ed3b0355d3e8cb00a6e5052bd8f5a05%2C; passport_auth_status_ss=0ed3b0355d3e8cb00a6e5052bd8f5a05%2C; uid_tt=69c7d375430ce09dcb1cb1cf518e6a67; uid_tt_ss=69c7d375430ce09dcb1cb1cf518e6a67; sid_tt=912aef384fa714ab420dcc7219690cc8; sessionid=912aef384fa714ab420dcc7219690cc8; sessionid_ss=912aef384fa714ab420dcc7219690cc8; IsDouyinActive=true; publish_badge_show_info=%220%2C0%2C0%2C1713889346348%22; LOGIN_STATUS=1; store-region=cn-js; store-region-src=uid; _bd_ticket_crypt_doamin=2; _bd_ticket_crypt_cookie=5bf9d5fe4c35914c6fd04455eb3740eb; __security_server_data_status=1; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCRXp0N2tQMEo0YWhoRHROSWFDYXZHRjFZZnZmbktxMU1OV01jTUdpaFpnaTd4OWlEbG9FcUJIMEluLy9TS2RHRXIzekVHbDF5aEViU2h1cDYremRuZ009IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D; passport_fe_beating_status=true; stream_player_status_params=%22%7B%5C%22is_auto_play%5C%22%3A0%2C%5C%22is_full_screen%5C%22%3A0%2C%5C%22is_full_webscreen%5C%22%3A0%2C%5C%22is_mute%5C%22%3A1%2C%5C%22is_speed%5C%22%3A1%2C%5C%22is_visible%5C%22%3A1%7D%22; sid_guard=912aef384fa714ab420dcc7219690cc8%7C1713889348%7C5183997%7CSat%2C+22-Jun-2024+16%3A22%3A25+GMT; sid_ucp_v1=1.0.0-KGViYWY3MDgwMDVjYTc3NDRkNWRlNDZhMDhlNWYzZjgzN2IxODhhMjUKGQiBi9DU8QIQxMCfsQYY7zEgDDgGQPQHSAQaAmhsIiA5MTJhZWYzODRmYTcxNGFiNDIwZGNjNzIxOTY5MGNjOA; ssid_ucp_v1=1.0.0-KGViYWY3MDgwMDVjYTc3NDRkNWRlNDZhMDhlNWYzZjgzN2IxODhhMjUKGQiBi9DU8QIQxMCfsQYY7zEgDDgGQPQHSAQaAmhsIiA5MTJhZWYzODRmYTcxNGFiNDIwZGNjNzIxOTY5MGNjOA; home_can_add_dy_2_desktop=%221%22; odin_tt=66ee26bf3137289be9e8c830f42d100b9a9d75723d1f4f59b61c7df0bcce2230d3401c5736a2017a1baa49c3412bacee; FOLLOW_LIVE_POINT_INFO=%22MS4wLjABAAAAkZPCpovrY2m0giobJDUhKHwWvC2XoszY3QTp9lMfAgA%2F1713974400000%2F1713889444415%2F1713889346587%2F0%22; msToken=Q3KDqooxbz-jTmoSpjfuewUN4LVs5uiaSHjFJgMrMaKNxE8dijG0ghR_Ilf9WXMcWeWG1hgwF6ceWa0mtJtPilT-VIdYfsMclAgnU39J76W-KJIVKmmG"
kwai_cookie = "did=web_b183f664d0b04d66873b9a14e32e5407; didv=1712914996000"
bilibili_cookie = ""

os.environ['page_wait'] = "20000"
os.environ['headless'] = "1"
